<!-- 用户列表 -->
<template>
  <div class="user-list">
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>
        <a href="/">用户管理</a>
      </el-breadcrumb-item>
      <el-breadcrumb-item>用户列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-tabs v-model="activeName" @tab-click="changeTabs">
        <el-tab-pane
          v-for="tabListData in tabListDatas"
          :key="tabListData.level"
          :label="tabListData.name"
          :name="tabListData.name"
        ></el-tab-pane>
      </el-tabs>
      <el-table :data="userLists.data" stripe border style="width: 100%">
        <el-table-column prop="id" label="序号" width="80"></el-table-column>
        <el-table-column prop="username" label="姓名" width="180"></el-table-column>
        <el-table-column prop="phone" label="手机号"></el-table-column>
        <el-table-column prop="sex" label="性别" width="80"></el-table-column>
        <el-table-column prop="age" label="年龄" width="80"></el-table-column>
        <el-table-column prop="email" label="邮箱"></el-table-column>
        <el-table-column prop="level" label="级别"></el-table-column>
        <el-table-column prop="is_show" label="是否可用">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.is_show"
              active-value="1"
              inactive-value="0"
              active-color="#13ce66"
              inactive-color="#ff4949"
              active-text="开"
              inactive-text="关"
            ></el-switch>
          </template>
        </el-table-column>
        <el-table-column prop="address" label="地址"></el-table-column>
        <el-table-column fixed="right" label="操作" width="160">
          <template slot-scope="scope">
            <el-button @click="handleDeleClick(scope.row)" type="danger" size="small">删除</el-button>
            <el-button type="primary" size="small" @click="openEditDialog(scope.row)">编辑</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage4"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="5"
        background
        layout="total, sizes, prev, pager, next, jumper"
        :total="userLists.count"
      ></el-pagination>
    </el-card>

    <!-- 修改用户弹窗 -->
    <el-dialog title="用户修改" :visible.sync="dialogEditFormVisible">
      <el-form :model="editForm" ref="ruleEditForm" status-icon :rules="editRules">
        <el-form-item label="年龄" prop="age" label-width="80px">
          <el-input v-model.number="editForm.age" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email" label-width="80px">
          <el-input v-model="editForm.email" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="地址" label-width="80px">
          <el-input v-model="editForm.address" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogEditFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitEditForm('ruleEditForm')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
//例如：import 《组件名称》 from '《组件路径》';
import tools from "@/utils/tools";
import { userEditApi, userDeleApi } from "@/api/user.js";
export default {
  //import引入的组件需要注入到对象中才能使用
  components: {},
  data() {
    // 自定义校验age
    var checkAge = (rule, value, callback) => {
      // value指的时表单中输入的值
      if (!value) {
        return callback(new Error("年龄不能为空"));
      }
      setTimeout(() => {
        if (!Number.isInteger(value)) {
          callback(new Error("请输入数字值"));
        } else {
          if (value < 18) {
            callback(new Error("必须年满18岁"));
          } else {
            callback();
          }
        }
      }, 1000);
    };
    var checkEmail = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("邮箱不能为空"));
      } else {
        if (!tools.regEmail.test(value)) {
          callback(new Error("邮箱格式不正确"));
        } else {
          callback();
        }
      }
    };
    //这里存放数据
    return {
      page: 1,
      size: 5,
      level: 0,
      activeName: "全部",
      currentPage4: 1,
      dialogEditFormVisible: false,
      editForm: {
        age: "",
        email: "",
        address: ""
      },
      tabListDatas: [
        {
          level: "0",
          name: "全部"
        },
        {
          level: "1",
          name: "管理员"
        },
        {
          level: "2",
          name: "青铜用户"
        },
        {
          level: "3",
          name: "白银用户"
        },
        {
          level: "4",
          name: "黄金用户"
        }
      ],
      editRules: {
        age: [{ validator: checkAge, trigger: "blur" }],
        email: [{ validator: checkEmail, trigger: "blur" }]
      }
    };
  },
  //监听属性 类似于data概念
  computed: {
    userLists() {
      return this.$store.state.user.userListDatas;
    }
  },
  //方法集合
  methods: {
    getUserListDatas() {
      this.$store.dispatch("actChangeUserListDatas", {
        page: this.page,
        size: this.size,
        level: this.level
      });
    },
    changeTabs(tab) {
      console.log(this.tabListDatas[tab.index].level);
      this.level = this.tabListDatas[tab.index].level;
      this.getUserListDatas();
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.page = 1;
      this.size = val;
      this.getUserListDatas();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.page = val;
      this.getUserListDatas();
    },
    // 打开修改用户窗口
    openEditDialog(rowData) {
      console.log(rowData);

      this.editForm = rowData;
      this.dialogEditFormVisible = true;
    },
    submitEditForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          // alert("submit!");
          userEditApi(this.editForm).then(result => {
            console.log(result);
            if (result.data.status == 200) {
              this.getUserListDatas();
              this.dialogEditFormVisible = false;
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    handleDeleClick(rowData) {
      this.$confirm("亲，您确定要删除吗?", "提示", {
        confirmButtonText: "残忍删除",
        cancelButtonText: "再用用",
        type: "warning"
      })
        .then(() => {
          userDeleApi(rowData).then(result => {
            console.log(result);
            if (result.data.status == 200) {
              this.getUserListDatas();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    }
  },
  mounted() {
    this.getUserListDatas();
  }
};
</script>
<style>
</style>